package com.dyh.quickdemo.demos.lbdAnd2;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.function.Consumer;

/*
@Author=Joey Wheeler
@Date=2020/12/25 11:18
*/
@Slf4j
public class M {

    @Test
    public void test(){
        csExe("helloWord!",(Consumer<String> & LogStartEnd &LogSpeed &Circuitbreaker) (log::info) );
    }

    public <T> void csExe(T t,Consumer<T> consumer){

        Consumer<T> enchanceCs = consumer;
        Class<?>[] itfs = consumer.getClass().getInterfaces();
        for (Class<?> itf : itfs) {
          if (itf==LogStartEnd.class){
              Consumer<T> finalTemp = enchanceCs;
              enchanceCs = e->{
                  log.info("开始");
                  finalTemp.accept(e);
                  log.info("结束");
              };
          }

            if (itf==LogSpeed.class){
                Consumer<T> finalTemp = enchanceCs;
                enchanceCs = e->{
                    long start = System.currentTimeMillis();
                    finalTemp.accept(e);
                    long end = System.currentTimeMillis();
                    log.info("耗时:{}",end-start);
                };
            }
        }

        if (enchanceCs==null)
            enchanceCs = consumer;

        enchanceCs.accept(t);
    }

    public interface  Enhance{}
    public interface  LogStartEnd extends Enhance{}
    public interface  LogSpeed extends Enhance{}
    public interface Circuitbreaker extends Enhance{}



//    public  enum Enhances implements Enhance{
//        LogSpeed,
//        LogStartEnd
//    }



}
